import {Component, ViewChild} from '@angular/core';
import {Events, IonicPage, Navbar, NavController} from 'ionic-angular';
import {FormBuilder, Validators} from "@angular/forms";
import {DialogProvider} from "@providers/dialog";
import {HttpProvider} from "@providers/http";
import {Util} from "@providers/util";
import {DataQueryServiceProvider} from "@providers/data-query-service/data-query-service";
import {Observable} from "rxjs/Observable";

/**
 * Generated class for the WorkerForgetPasswordPage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: 'page-worker-forget-password',
  templateUrl: 'worker-forget-password.html',
})
export class WorkerForgetPasswordPage {

  @ViewChild(Navbar) navbar: Navbar;

  tel: any;
  verificationCode: any;
  newPassword: any;
  checkPassword: any;
  btnTitle: any = '获取验证码';
  isSend: boolean = false;
  isFirstStep: boolean = true;

  constructor(private dataQueryService: DataQueryServiceProvider, private dialog: DialogProvider,
              private http: HttpProvider, public navCtrl: NavController, private formBuilder: FormBuilder,
              private events: Events) {
  }

  checkForm = this.formBuilder.group({
    'tel': ['', [Validators.required]],
    'verificationCode': ['', [Validators.required]]
  });
  //form验证机制
  forgetForm = this.formBuilder.group({
    'newPassword': ['', [Validators.required, Validators.minLength(6)]],
    'checkPassword': ['', [Validators.required, Validators.minLength(6)]]
  });

  ionViewDidLoad() {
    Util.replaceBackButtonEvent(this.navbar, this.navCtrl, this.events);
  }

  //修改
  forget() {
    if (this.isFirstStep) {
      this.dataQueryService.getkey(this.tel, this.verificationCode).subscribe(message => {
          if (message.MESSAGE === '验证码验证成功!'|| message.MSGID ==='S' ) {
            this.isFirstStep = false;
          }else{
            this.isFirstStep = true;
            this.dialog.showAlert("验证码错误");
          }
        },
        error => {
          this.dialog.showAlert("验证码错误");
        });
    } else {
      if (this.newPassword != this.checkPassword) {
        this.dialog.showAlert('两次输入密码不一致！');
        return;
      }
      this.dialog.showLoading();
      this.http.post("/Inbound/invoke.do", {
        interfaceId: "csm.login.fgtpw.up",
        data: JSON.stringify({"IDVALUE": Number(this.tel), "NEWPASSWORD": this.newPassword})
      }).subscribe(
        message => {
          this.dialog.hideLoading();
          this.dialog.showAlert('修改成功!').onDidDismiss((item) => {
            this.navCtrl.pop();
          });
        },
        error => {
          this.dialog.hideLoading();
          this.dialog.showAlert('修改密码失败');
        });
    }
  }

  //获取验证码
  getVerificationCode() {
    if (this.tel == null || this.tel == '') {
      this.dialog.showAlert('请填写手机号码');
      return;
    }
    if (this.isSend) {
      return;
    }
    this.dialog.showLoading();
    this.http.post("/trust/Inbound/invoke.do", {
      interfaceId: "csm.login.putyzm",
      data: JSON.stringify({'mobile': this.tel})
    }).subscribe(message => {
      this.dialog.hideLoading();
      this.isSend = true;
      let ms = 60;
      let sub = Observable.interval(1000).subscribe(() => {

        ms -= 1;
        this.btnTitle = ms + "秒";
        if (ms == 0) {
          this.btnTitle = '获取验证码';
          this.isSend = false;
          sub.unsubscribe();
        }
      });
      this.dialog.showAlert('获取验证码成功，请耐心等待');
    }, error => {
      this.dialog.showAlert('获取验证码失败!');
      this.dialog.hideLoading();
    });
  }

}
